package repository

import (
	"gorm.io/gorm"

	"forum/pkg/domain"
)

type profileRepository struct {
	DB *gorm.DB
}

func NewProfileRepository(db *gorm.DB) domain.ProfileReposiroty {
	return &profileRepository{}
}

func (repo *profileRepository) SaveProfile(profile *domain.Profile) error {
	return repo.DB.Save(profile).Error
}

func (repo *profileRepository) GetProfileByUserID(userID string) (*domain.Profile, error) {
	profile := &domain.Profile{}
	err := repo.DB.First(profile, "id = ?", userID).Error
	return profile, err
}

func (repo *profileRepository) GetUserByID(userID string) (*domain.User, error) {
	user := &domain.User{}
	err := repo.DB.First(user, "id = ?", userID).Error
	return user, err
}

func (repo *profileRepository) GetProfileByName(username string) (*domain.Profile, error) {
	profile := &domain.Profile{}
	err := repo.DB.Where("username = ?", username).First(profile).Error
	return profile, err
}
